package drone;

import com.jme3.app.SimpleApplication;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import java.util.ArrayList;
import java.util.Random;

/*
 * @author Kjell Bunjes, Robert Verginien Nickel
 */
public class Main extends SimpleApplication implements ActionListener {
    private Controller ctr;
    private Drone drone;
    private ArrayList<Car> cars = new ArrayList<Car>();

    private Barrier barrier;
    private SpacePainter sp;
    private DataBaseSpaces dbs;
    private ArrayList<Coordinates> route;

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }
    @Override
    public void simpleInitApp() {
        drone = new Drone(assetManager);
        barrier = new Barrier(assetManager, this.getRootNode());
        ctr = Controller.getInstance(drone);
        dbs = DataBaseSpaces.getInstance();
        sp = new SpacePainter(assetManager, this.getRootNode());
        setUpKeys();
        //60% of the spaces are occopied from the beginning
        preInit(60);
        rootNode.attachChild(drone);
        flyCam.setMoveSpeed(6);
        
        cam.setLocation(new Vector3f(12f, 12f, -18f));
        Spatial s = getRootNode().getChild(7);
        Vector3f vec = s.getLocalTranslation();
        cam.lookAt(vec, new Vector3f(0, 1, 0));
    }

    /**
     * @param tpf time per frame - used to make animations independent on the variance in FPS
     */
    @Override
    public void simpleUpdate(float tpf) {
        //this method calls the update for the..
        //..drone
        drone.update(tpf);
        //..barrier
        barrier.update(tpf);
        //.. and every car.
        for(int i = 0; i<cars.size(); i++){
            cars.get(i).update(tpf);
        }
        //Moreover it looks for the next car, that is about to follow the drone
        for(int j = 0; j<cars.size(); j++){
            if(!cars.get(j).isParked()){
                cars.get(j).setFirst(true);
                break;
            }
        }
    }

    /**
    * Defines KEY_SPACE as the key to be pressed to create a new car
    */
    private void setUpKeys() {
        inputManager.addMapping("Car", new KeyTrigger(keyInput.KEY_SPACE));
        inputManager.addListener(this, "Car");
    }

    /**
     * @param name 
     * @param keyPressed 
     * @param tpf time-per-frame
     */
    public void onAction(String name, boolean keyPressed, float tpf) {
        if(name.equals("Car") && !keyPressed){
            cars.add(new Car(assetManager, drone, this.getRootNode(), new Coordinates(10.0f,-8.0f), barrier, false));
        }
    }
    /**
     * Preinits the parking-space random with cars
     * @param percent the amount of the spaces that are occupied
     */
    private void preInit(float percent){
        Random r = new Random();
        for(int i = 0; i < DataBaseSpaces.getInstance().getAllSpaces().size(); i++){
            if(r.nextFloat() < percent / 100){
                DataBaseSpaces.getInstance().getAllSpaces().get(i).setEmpty(false);
                float x = DataBaseSpaces.getInstance().getAllSpaces().get(i).getCoordinates().getxPos();
                float z = DataBaseSpaces.getInstance().getAllSpaces().get(i).getCoordinates().getzPos();
                Car car = new Car(assetManager, drone, this.getRootNode(), new Coordinates(x,z), barrier, true);
                car.setStatus(CarStatus.PARK);
                cars.add(car);
            }
        }
    }
}
